package com.hzdesk.media.koreabatchmatvector.custom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.hzdesk.media.koreabatchmatvector.bean.DcfBean;
import com.hzdesk.media.koreabatchmatvector.bean.PathBean;
import com.hzdesk.media.koreabatchmatvector.bean.SerPoint;
import com.hzdesk.media.koreabatchmatvector.bean.XmlVideoFile;
import com.hzdesk.media.koreabatchmatvector.config.AppConfig;
import com.hzdesk.media.koreabatchmatvector.contral.LineCtl;
import com.hzdesk.media.koreabatchmatvector.contral.OvaluCtl;
import com.hzdesk.media.koreabatchmatvector.contral.PenuCtl;
import com.hzdesk.media.koreabatchmatvector.contral.RectuCtl;
import com.hzdesk.media.koreabatchmatvector.inter.ISketchpadDraw;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/29.
 */

public class FirstCanvas extends View{
    private String TAG=FirstCanvas.class.getSimpleName();
    private Canvas mCanvas;
    private Bitmap bitmap;
    private DcfBean mBean;
    private Paint mPaint;
    private List<PathBean> mList;
    private long currentTime=0;
    private long startTime=0;
    private ISketchpadDraw m_curTool;
    private boolean timeIsChange=false;
    private boolean pathbeanIsChange=false;
    private PathBean currentPathBean;
    private long currentPathBeanTime=0;
    private List<XmlVideoFile> xmlVideoFileList;
    private boolean isTouchUp=false;

    public FirstCanvas(Context context) {
        super(context);
        initialize();
    }

    public FirstCanvas(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public FirstCanvas(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize();
    }
    private void initialize(){
        mPaint=new Paint();
        bitmap = Bitmap.createBitmap(AppConfig.SCREEN_WIDTH, AppConfig.SCREEN_HIGHR, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas();
        mCanvas.setBitmap(bitmap);
        xmlVideoFileList=new ArrayList<>();
    }

    public void startDraw(DcfBean bean,String name){
        mBean=bean;
        mList=bean.getList();
        xmlVideoFileList=bean.getFileList();
        //开始时间
        for (XmlVideoFile xmlVideoFile:xmlVideoFileList){
            if (name.equals(xmlVideoFile.getName())){
                startTime=xmlVideoFile.getStartTime();
                break;
            }
        }
        new MyThread().start();
    }
    public void changeProgress(long time){
        currentTime=time+startTime;
        timeIsChange=true;
    }

    private class MyThread extends Thread{
        List<Long> list_time=new ArrayList<>();

        public MyThread() {
        }
        private boolean isDraw=false;
        @Override
        public void run() {
            super.run();
            while (true){
                if (timeIsChange){
                    timeIsChange=false;
                    isDraw=false;
                    for (PathBean bean: mList){
                        //Undo Redo Clear 跳出
                        if (bean.getType().equals(AppConfig.PAINT_UNDO)||bean.getType().equals(AppConfig.PAINT_REDO)||bean.getType().equals(AppConfig.PAINT_CLEAR)){
                            continue;
                        }

                        //是否当前路径
                        if (currentPathBeanTime==bean.getTime()){
                            pathbeanIsChange=false;

                        }else {
                            pathbeanIsChange=true;
                            currentPathBean=bean;
                            currentPathBeanTime=bean.getTime();
                            //渲染画笔
                            getPaint(bean.getColor(),bean.getSize());
                            //清空时间集合
                            list_time=null;

                            //遍历时间点
                            list_time=bean.getmSerPath().mTimes;

                            //未到时间跳过
                            if (currentTime<list_time.get(0)){
                                continue;
                            }

                            //超过时间跳出
                            if (currentTime>list_time.get(list_time.size()-1)){
                                continue;
                            }

                            if (bean.getType().equals(AppConfig.PAINT_PENU)){
                                m_curTool=new PenuCtl(mPaint);

                            }
                            if (bean.getType().equals(AppConfig.PAINT_LINE)){
                                m_curTool=new LineCtl(mPaint);

                            }
                            if (bean.getType().equals(AppConfig.PAINT_OVAL)){
                                m_curTool=new OvaluCtl(mPaint);

                            }
                            if (bean.getType().equals(AppConfig.PAINT_RECT)){
                                m_curTool=new RectuCtl(mPaint);

                            }
                        }
                        for (int i=0;i<list_time.size();i++){

                            if (currentTime>list_time.get(i)){
                                continue;
                            }
                            if (pathbeanIsChange){
                                isTouchUp=false;
                                SerPoint serPoint=bean.getmSerPath().mPoints.get(0);
                                m_curTool.touchDown(serPoint.x,serPoint.y);
                                Log.e(TAG,"-----touchDown-----");
                            }
                            if (currentTime==list_time.get(i)){
                                SerPoint serPoint=bean.getmSerPath().mPoints.get(i);
                                if(i==(list_time.size()-1)){
                                    isTouchUp=true;
                                    m_curTool.touchUp(serPoint.x,serPoint.y);
                                    m_curTool.draw(mCanvas);
                                    Log.e(TAG,"-----touchUp-----");
                                }else {
                                    m_curTool.touchMove(serPoint.x,serPoint.y);
                                   // Log.d(TAG,"-----move-----");

                                }
                                isDraw=true;
                                break;
                            }
                        }
                        if (isDraw){
                            //m_curTool.draw(mCanvas);
                            postInvalidate();
                            break;
                        }

                    }


                }
            }

        }
    }
    private void getPaint(String color, int size) {
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.parseColor("#" + color));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(size);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(bitmap, 0, 0, mPaint);
        super.onDraw(canvas);
       /* canvas.drawBitmap(bitmap, 0, 0, mPaint);
        mCanvas.setBitmap(bitmap);
        Paint paint = new Paint();
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        mCanvas.drawPaint(paint);*/
        if (m_curTool!=null){   //调用绘图功能
            if (!isTouchUp){
                m_curTool.draw(canvas);
            }
        }
    }
}
